const path = require('path')
const webpack = require('webpack')
    // const GitRevisionPlugin = require('git-revision-webpack-plugin')
    // const GitRevision = new GitRevisionPlugin()
    // const buildDate = JSON.stringify(new Date().toLocaleString())
const createThemeColorReplacerPlugin = require('./config/plugin.config')

function resolve(dir) {
    return path.join(__dirname, dir)
}

// check Git
// function getGitHash () {
//   try {
//     return GitRevision.version()
//   } catch (e) {}
//   return 'unknown'
// }

const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
    // webpack build externals
    externals: {
        vue: 'Vue',
        'vue-router': 'VueRouter',
        vuex: 'Vuex',
        axios: 'axios'
    },
    css: [],
    // https://unpkg.com/browse/vue@2.6.10/
    js: []
}

// vue.config.js
const vueConfig = {
    publicPath: '/mscp/',
    outputDir: 'mscp',
    configureWebpack: {
        // webpack plugins
        plugins: [
            // Ignore all locale files of moment.js
            // new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
            // new webpack.DefinePlugin({
            //   APP_VERSION: `"${require('./package.json').version}"`,
            //   GIT_HASH: JSON.stringify(getGitHash()),
            //   BUILD_DATE: buildDate
            // })
        ],
        // if prod, add externals
        // externals: isProd ? assetsCDN.externals : {}
        externals: isProd ? {} : {}
    },

    chainWebpack: (config) => {
        config.resolve.alias
            .set('@$', resolve('src'))
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        svgRule
            .oneOf('inline')
            .resourceQuery(/inline/)
            .use('vue-svg-icon-loader')
            .loader('vue-svg-icon-loader')
            .end()
            .end()
            .oneOf('external')
            .use('file-loader')
            .loader('file-loader')
            .options({
                name: 'assets/[name].[hash:8].[ext]'
            })

        // if prod is on
        // assets require on cdn
        if (isProd) {
            config.plugin('html').tap(args => {
                args[0].cdn = assetsCDN
                return args
            })
        }
    },

    css: {
        requireModuleExtension: true,
        extract: false,
        sourceMap: false,
        loaderOptions: {//#1396db
            less: {
                // If you are using less-loader@5 please spread the lessOptions to options directly
                //深蓝 0e5d9c child #08528e  浅蓝 00a2e9 黄色 fbc616
                modifyVars: {
                    'primary-color': '#00a2e9',//#03929f 
                    'link-color': '#00a2e9',//#03929f
                    'border-radius-base': '2px',
                    'menu-dark-submenu-bg': '#08528e', //菜单展开颜色  屏蔽恢复默认色
                    'layout-header-background':'#0e5d9c',  //菜单背景色#0c4d68   屏蔽恢复默认色
                    'layout-header-bg':'#0e5d9c',  //header栏背景色#03929f   屏蔽恢复默认色
                    'layout-header-text':'#fff',  //header栏背景色#03929f   屏蔽恢复默认色
                    'font-size-base': '12px',
                    'font-size-sm': '10px',
                },
                javascriptEnabled: true,
            }
        }
    },

    devServer: {
        // development server port 8000
        port: 8000
            // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
            // proxy: {
            //   '/api': {
            //     target: 'https://mock.ihx.me/mock/5baf3052f7da7e07e04a5116/antd-pro',
            //     ws: false,
            //     changeOrigin: true
            //   }
            // }
    },

    // disable source map in production
    productionSourceMap: false,
    lintOnSave: undefined,
    // babel-loader no-ignore node_modules/*
    transpileDependencies: []
}

// preview.pro.loacg.com only do not use in your production;
if (process.env.VUE_APP_PREVIEW === 'true') {
    // console.log('VUE_APP_PREVIEW', true)
    // add `ThemeColorReplacer` plugin to webpack plugins
    vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}

module.exports = vueConfig